﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace SubCom_Doc2Pdf
{
    internal abstract class Convertor
    {
        protected readonly string SUCCESS = "success";

        private readonly Target target;

        protected Convertor(Target target)
        {
            this.target = target;
        }

        public void DoProcess()
        {
            PreProcess(target);
            OnProcess(target);
            AfterProcess(target);
        }

        protected abstract void OnProcess(Target target);

        protected void SetTargetApps(List<string> apps)
        {
            target.TargetApps = apps;
        }

        private void PreProcess(Target target)
        {
            CleanUp(target);
            IsFileExits(target.Sources);
        }

        private void AfterProcess(Target target)
        {
            CleanUp(target);
        }

        protected static void IsFileExits(string path)
        {
            if (Path.GetPathRoot(path).Equals(string.Empty) || !File.Exists(path))
            {
                throw new FileNotFoundException();
            }
        }

        private void CleanUp(Target target)
        {
            foreach (var targetApp in target.TargetApps)
            {
                var pros = Process.GetProcessesByName(targetApp);
                foreach (var p in pros)
                {
                    p.Kill();
                }
            }
        }

        protected void ThrowException(Exception ex)
        {
            Console.WriteLine("ERROR:" + ex);
        }
    }
}